'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 竞技场
 * @author agl
 */

var dataApi = require('../libs/dataApi');
var _ = require('lodash');
var constantUtil = require('../util/constantUtil');

/**
 * 取出竞技场基本配置信息
 */
exports.getArenaConf = function (npc_id) {
    return dataApi.arena.findById(npc_id);
};

/**
 * 按排名取出排名区间信息
 * @return {[type]}     [description]
 */
exports.getRangeConf = function (rank) {
    var result = null;
    dataApi.arena_prize.any(function (item) {
        if (item[0].min_cup <= rank && rank <= item[0].max_cup) {
            result = item;
            return true;
        }
        return false;
    });
    return result;
};

/**
 * 取出最高排名
 * @return {[type]}     [description]
 */
exports.getMaxRank = function () {
    var max_rank = 0;
    dataApi.arena_prize.any(function (item) {
        if (max_rank < item[0].max_cup) {
            max_rank = item[0].max_cup;
        }
        return false;
    });

    return max_rank;
};

/**
 * 固定奖励
 * @return {[type]}     [description]
 */
exports.getFixedReward = function (rank) {
    return dataApi.arena_fixed_prize.find(function (o) {
        return true;
    });
};

var shop_rate_info = [];
var total_prob = 0;
var loaded = false;
var must_appear_list = [];

/**
 * 获取商城物品概率信息
 * @return {[type]}     [description]
 */
var _loadShopInfo = function () {
    if (loaded) {
        return;
    }
    loaded = true;
    shop_rate_info = [];
    total_prob = 0;
    var shop_conf = {};
    var shop_pos = 0;
    dataApi.arena_shop.any(function (item) {
        if (!!item.must_appear) {
            shop_pos++;
            must_appear_list.push({
                shop_pos: shop_pos,
                shop_id: item.id,
                count: item.buy_times
            });
        } else {
            total_prob += item.probability;
            shop_rate_info.push({
                shop_id: item.id,
                sum_rate: total_prob,
                count: item.buy_times
            });
        }
    });

    var arena_shop_goods_num = constantUtil.value('arena_shop_goods_num');
    if (must_appear_list.length > arena_shop_goods_num) {
        must_appear_list = _.slice(must_appear_list, 0, arena_shop_goods_num);
    }
};

/**
 * 获取商城物品概率信息
 * @return {[type]}     [description]
 */
exports.getShopRateInfo = function () {
    _loadShopInfo();
    return shop_rate_info;
};

/**
 * 获取商城物品概率信息
 * @return {[type]}     [description]
 */
exports.getTotalProb = function () {
    _loadShopInfo();
    return total_prob;
};

var first_rank_list = [];

var _loadFirstRankList = function () {
    if (first_rank_list.length > 0) {
        return;
    }

    var list = [];
    var lower_rank = 1;
    dataApi.arena_first_prize.any(function (item_list) {
        var first_item = item_list[0];
        var reward_list = [];
        _.forEach(item_list, function (item) {
            reward_list.push({
                reward_type: item.reward_type,
                reward: item.reward,
                reward_num: item.reward_num,
            });
        });
        list.push({
            lower_rank: lower_rank,
            upper_rank: first_item.id,
            reward_list: reward_list
        });
        lower_rank = first_item.id + 1;
    });
    list.push({
        lower_rank: lower_rank,
        upper_rank: 999999,
    });

    for (var idx = list.length - 1; idx >= 0; idx--) {
        first_rank_list.push(list[idx]);
    }
};

var _findRangeIdx = function (rank, from_idx) {
    _loadFirstRankList();
    var found_idx = -1;
    for (var idx = from_idx; idx < first_rank_list.length; idx++) {
        var rank_item = first_rank_list[idx];
        if (rank_item.lower_rank <= rank && rank <= rank_item.upper_rank) {
            found_idx = idx;
            break;
        }
    }

    return found_idx;
};

/**
 * 取出历史最高排名奖励
 * @return {[type]}     [description]
 */
exports.getFirstRankReward = function (from_rank, to_rank) {
    // 查找旧排名区间
    var from_idx = _findRangeIdx(from_rank, 0);
    if (from_idx < 0) {
        console.error('---config arena_first_prize error for from_rank:', from_rank);
        return [];
    }

    // 查找新排名区间
    var to_idx = _findRangeIdx(to_rank, from_idx);
    if (from_idx < 0) {
        console.error('---config arena_first_prize error for to_rank:', to_rank);
        return [];
    }
    if (to_idx <= from_idx) {
        return [];
    }

    var _append_list = function (item_map, add_list) {
        _.forEach(add_list, function (item) {
            var key = item.reward * 100 + item.reward_type;
            if (key in item_map) {
                item_map[key] += item.reward_num;
            } else {
                item_map[key] = item.reward_num;
            }
        });
    };

    var reward_map = {};
    for (var idx = from_idx + 1; idx <= to_idx; idx++) {
        var list = first_rank_list[idx].reward_list;
        _append_list(reward_map, list);
    }
    var reward_list = [];
    _.forEach(reward_map, function (item, key) {
        key = +key;
        var reward_type = key % 100;
        var reward = (key - reward_type) / 100;
        reward_list.push({
            reward_type: reward_type,
            reward: reward,
            reward_num: item
        });
    });

    return reward_list;
};

/**
 * 取出商城物品信息
 */
exports.getShopItem = function (shop_id) {
    return dataApi.arena_shop.findById(shop_id);
};

/**
 * 取出商城物品信息
 */
exports.getMustAppearList = function (shop_id) {
    _loadShopInfo();
    return must_appear_list;
};
